﻿using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Windows;
using Microsoft.Practices.Prism.MefExtensions;
using Microsoft.Practices.Prism.Regions;
using SMSManager.Data;
using SMSManager.SMSServices;
using SMSManager.WpfClient.Properties;
using SMSManager.WpfClient.Views;

namespace SMSManager.WpfClient
{
	public class Bootstrapper : MefBootstrapper
	{
		internal CompositionContainer CompositionContainer
		{
			get { return Container; }
		}

		protected override void ConfigureAggregateCatalog()
		{
			base.ConfigureAggregateCatalog();

			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (User).Assembly));
			AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof (LGUPlusSMSService).Assembly));
		}

		protected override void ConfigureContainer()
		{
			base.ConfigureContainer();
			Container.ComposeExportedValue(Container);
		}

		protected override DependencyObject CreateShell()
		{
			var shell = new Shell();
			return shell;
		}

		protected override void InitializeShell()
		{
			base.InitializeShell();

			RegisterViews();

			Application.Current.MainWindow = (Shell) Shell;
			Application.Current.MainWindow.Show();
		}

		private void RegisterViews()
		{
			var regionManager = Container.GetExportedValue<IRegionManager>();

			//register to MainRegion
			regionManager.RegisterViewWithRegion(Resources.MainRegionName, typeof (SMSView));

			//register at SMSView
			regionManager.RegisterViewWithRegion(Resources.SMSServiceListRegionName, typeof (SMSServiceListView));
		}
	}
}